package com.rcloud.stock.ui.detail;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.rcloud.stock.bean.Gg;
import com.rcloud.stock.bean.Zx;
import com.rcloud.stock.net.ServerAddress;
import com.rcloud.stock.net.VolleySingleton;

import java.util.Arrays;
import java.util.List;

public class DetailViewModel extends AndroidViewModel {
    public MutableLiveData<List<Zx>> zxLive = new MutableLiveData<>();
    public MutableLiveData<List<Gg>> ggLive = new MutableLiveData<>();
    public MutableLiveData<String> articleLive = new MutableLiveData<>();
    public DetailViewModel(@NonNull Application application) {
        super(application);
    }

    public void fetchZx(String code) {
        StringRequest request = new StringRequest(Request.Method.GET,
                ServerAddress.ROOT + "get_zx?code=" + code,
                response -> {
                    Gson gson = new Gson();
                    Zx[] zxes = gson.fromJson(response, Zx[].class);
                    zxLive.setValue(Arrays.asList(zxes));
                }, error -> {
        });
        request.setRetryPolicy(new DefaultRetryPolicy(60000,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        RequestQueue queue = VolleySingleton.getInstance(getApplication()).getRequestQueue();
        queue.add(request);
    }

    public void fetchGg(String code) {
        StringRequest request = new StringRequest(Request.Method.GET,
                ServerAddress.ROOT + "get_gg?code=" + code,
                response -> {
                    Gson gson = new Gson();
                    Gg[] ggs = gson.fromJson(response, Gg[].class);
                    ggLive.setValue(Arrays.asList(ggs));
                }, error -> {
        });
        request.setRetryPolicy(new DefaultRetryPolicy(60000,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        RequestQueue queue = VolleySingleton.getInstance(getApplication()).getRequestQueue();
        queue.add(request);
    }

    public void fetchZxByUrl(String url) {
        StringRequest request = new StringRequest(Request.Method.GET,
                ServerAddress.ROOT + "get_zx_by_url?url=" + url,
                response -> {
                    articleLive.setValue(response);
                }, error -> {
        });
        request.setRetryPolicy(new DefaultRetryPolicy(60000,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        RequestQueue queue = VolleySingleton.getInstance(getApplication()).getRequestQueue();
        queue.add(request);
    }

    public void fetchGgByUrl(String url) {
        StringRequest request = new StringRequest(Request.Method.GET,
                ServerAddress.ROOT + "get_gg_by_url?url=" + url,
                response -> {
                    articleLive.setValue(response);
                }, error -> {
        });
        request.setRetryPolicy(new DefaultRetryPolicy(60000,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        RequestQueue queue = VolleySingleton.getInstance(getApplication()).getRequestQueue();
        queue.add(request);
    }
}
